﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace DoAnHuongRung
{
    public partial class frmLapLichLamViec : Form
    {
        public frmLapLichLamViec()
        {
            InitializeComponent();
        }

        private void frmLapLichLamViec_Load(object sender, EventArgs e)
        {
            dateTimePicker1.Value = DateTime.Now;
            LoadCmbCaLamViec();
            LoadCmbNhanVien();
            LoadCmbKhuVuc();
            LoadDtgLichLamViec();
        }

        private void LoadDtgLichLamViec()
        {
            if (cmbCaLamViec.SelectedValue != null && dateTimePicker1.Value != null)
            {
                int CaLamViec = int.Parse(cmbCaLamViec.SelectedValue.ToString());
                string Ngay = dateTimePicker1.Value.ToString("MM/dd/yyyy");
                dtgLichLamViec.AutoGenerateColumns = false;
                dtgLichLamViec.DataSource = LichLamViecBUS.Select_by_CaLamViec_Ngay(CaLamViec, Ngay);
            }
        }

        private void LoadCmbCaLamViec()
        {
            cmbCaLamViec.DisplayMember = "TenCa";
            cmbCaLamViec.ValueMember = "MaCa";
            cmbCaLamViec.DataSource = CaLamViecBUS.SelectAll();
        }

        private void LoadCmbNhanVien()
        {
            if (cmbCaLamViec.SelectedValue != null && dateTimePicker1.Value != null)
            {
                int CaLamViec = int.Parse(cmbCaLamViec.SelectedValue.ToString());
                string Ngay = dateTimePicker1.Value.ToString("MM/dd/yyyy");
                cmbNhanVien.DisplayMember = "TenNhanVien";
                cmbNhanVien.ValueMember = "MaNhanVien";
                cmbNhanVien.DataSource = LichLamViecBUS.Select_NhanVien_not_exists_in_CaLamViec_Ngay(CaLamViec, Ngay);
            }
        }

        private void LoadCmbKhuVuc()
        {
            cmbKhuVuc.DisplayMember = "TenKhuVuc";
            cmbKhuVuc.ValueMember = "MaKhuVuc";
            cmbKhuVuc.DataSource = KhuVucBUS.SelectAll();
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            LoadCmbNhanVien();
            LoadDtgLichLamViec();
        }

        private void cmbCaLamViec_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadCmbNhanVien();
            LoadDtgLichLamViec();
        }

        private void btnLamMoi_Click(object sender, EventArgs e)
        {
            dateTimePicker1.Value = DateTime.Now;
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            try
            {
                bool b = LichLamViecBUS.Delete(int.Parse(dtgLichLamViec.CurrentRow.Cells[0].Value.ToString()));
                LoadDtgLichLamViec();
                LoadCmbNhanVien();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (DateTime.Parse(dateTimePicker1.Value.ToString("MM/dd/yyyy")) >= DateTime.Parse(DateTime.Now.ToString("MM/dd/yyyy")))
            {
                try
                {
                    LichLamViecDTO llv = new LichLamViecDTO();
                    llv.MaNhanVien = int.Parse(cmbNhanVien.SelectedValue.ToString());
                    llv.MaCa = int.Parse(cmbCaLamViec.SelectedValue.ToString());
                    llv.MaKhuVuc = int.Parse(cmbKhuVuc.SelectedValue.ToString());
                    llv.Ngay = dateTimePicker1.Value.ToString("MM/dd/yyyy");
                    bool b = LichLamViecBUS.Insert(llv);
                    LoadDtgLichLamViec();
                    LoadCmbNhanVien();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
                MessageBox.Show("Không thể lập lịch làm việc cho ngày cũ !!!");
            }
        }
    }
}
